package sk.naive.talker.command;

import sk.naive.talker.*;
import sk.naive.talker.adapter.TagConsts;
import sk.naive.talker.message.DefaultMessageFactory;
import sk.naive.talker.util.*;
import sk.naive.talker.props.*;

import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.*;

/**
 *
 * @author <a href="mailto:virgo@naive.deepblue.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.24 $ $Date: 2005/01/25 21:57:04 $
 */
public class PermissionGrant extends AbstractPermissionCommand {
	protected void noParameter(User victim) throws RemoteException, PropertyStoreException {
		user.send(permissionStatus(victim));
	}

	protected void withParameter(User victim, String permString) throws RemoteException, CommandException, PropertyStoreException {
		Utils.checkPermission(user, Consts.PERM_SUPERUSER);
		Property permProp = victim.getProperty(getPropertyName());
		Set perms = (Set) victim.get(getPropertyName());
		String[] permArray = Utils.splitWords(permString, -1);
		for (int i = 0; i < permArray.length; i++) {
			String foundPerm = Utils.findFirstInCollection(permArray[i], permProp.validValues(), true);
			if (foundPerm == null) {
				ctxSet(DefaultMessageFactory.CTXKEY_VAL, permArray[i]);
				sendHelper().sendMessage(user, "permissionGrant.noSuchPermission");
				continue;
			}
			ctxSet(DefaultMessageFactory.CTXKEY_VAL, foundPerm);
			ctxSet(DefaultMessageFactory.CTXKEY_VAL + "Vctm", victim.getProperties());
			if (perms.contains(foundPerm)) {
				perms.remove(foundPerm);
				sendHelper().sendMessage(user, "permissionGrant.denied");
				logger().log(Level.INFO, "User {0} denied permission {1} to user {2}.", new Object[] {user.getLogin(), foundPerm, victim.getLogin()});
			} else {
				perms.add(foundPerm);
				sendHelper().sendMessage(user, "permissionGrant.granted");
				logger().log(Level.INFO, "User {0} granted permission {1} to user {2}.", new Object[] {user.getLogin(), foundPerm, victim.getLogin()});
			}
			/* TODO notify - plus hlasky pre ineho usera
			actionNotify(user, victim, "password.successful.victim");
			*/
			victim.set(getPropertyName(), perms);
		}
	}

	protected String getPropertyName() {
		return PermissionProperty.UPROP_PERMISSION;
	}

	protected String getPermissionName() {
		return Consts.PERM_SUPERUSER;
	}

	protected String permissionStatus(User victim) throws RemoteException, PropertyStoreException {
		StringBuilder sb = new StringBuilder();
		Set perms = (Set) victim.get(getPropertyName());
		for (String perm : (Set<String>) user.getProperty(getPropertyName()).validValues()) {
			if (perms.contains(perm)) {
				Utils.addToBuffer(sb, perm, ", ");
			}
		}

		if (sb.length() == 0) {
			sb.append(getString("message.none", user.getProperties()));
		}

		if (user.equals(victim)) {
			sb.insert(0,getString("permissionGrant.userList", user.getProperties()));
		} else {
			ctxSet(DefaultMessageFactory.CTXKEY_VAL + "Vctm", victim.getProperties());
			sb.insert(0,getString("permissionGrant.victimList", user.getProperties()));
		}
		sb.append(Utils.tag(TagConsts.BR));
		return sb.toString();
	}
}
